package com.znsb.msfq.utils;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.znsb.msfq.R;
import com.znsb.msfq.app.ZnsbApp;

/**
 * @name：
 * @author： 孙科技
 * @phone： 13308334117
 * @createTime： 2017/2/8
 * @modifyTime： 2017/2/8
 * @explain：
 */

public class DialogUtils {

    public static void showPromptDialog(Activity activity, String title, String content, String leftMenu,
                                        String centerMenu, String rightMenu, final OnMenuClick onMenuClick) {
        showPromptDialog(activity, title, content, leftMenu, centerMenu, rightMenu, onMenuClick, null);
    }

    public static void showPromptDialog(Activity activity, String title, String content, String leftMenu,
                                        String centerMenu, String rightMenu, final OnMenuClick onMenuClick, String content2) {
        try {
            content = TextUtils.isEmpty(content) ? "系统提示" : content;
            final Dialog dialog = new Dialog(activity, R.style.dialog);
            dialog.show();
            View view = LayoutInflater.from(ZnsbApp.mContext).inflate(R.layout.common_prompt_dialog, null);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
            TextView textTitle = (TextView) view.findViewById(R.id.tv_common_prompt_title);
            TextView textContent = (TextView) view.findViewById(R.id.tv_common_prompt_content);

            TextView textContent2 = (TextView) view.findViewById(R.id.tv_common_prompt_content2);

            if (TextUtils.isEmpty(title)) {
                textTitle.setVisibility(View.GONE);
            } else {
                textTitle.setText(title);
                textTitle.setVisibility(View.VISIBLE);
            }
            if (TextUtils.isEmpty(content)) {
                textContent.setVisibility(View.GONE);
            } else {
                textContent.setText(content);
                textContent.setVisibility(View.VISIBLE);
            }

            if (TextUtils.isEmpty(content2)) {
                textContent2.setVisibility(View.GONE);
            } else {
                textContent2.setVisibility(View.VISIBLE);
                textContent2.setText(content2);
            }
            TextView textLeft = (TextView) view.findViewById(R.id.tv_common_prompt_left);
            TextView textCenter = (TextView) view.findViewById(R.id.tv_common_prompt_center);
            TextView textRight = (TextView) view.findViewById(R.id.tv_common_prompt_right);

            View viewOne = view.findViewById(R.id.view_one);
            View viewTwo = view.findViewById(R.id.view_two);

            if (!TextUtils.isEmpty(leftMenu)) {// 左菜单不为空
                textLeft.setVisibility(View.VISIBLE);
                textLeft.setText(leftMenu);
            } else {
                textLeft.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(centerMenu)) {
                textCenter.setVisibility(View.VISIBLE);
                textCenter.setText(centerMenu);
            } else {
                textCenter.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(rightMenu)) {
                textRight.setVisibility(View.VISIBLE);
                textRight.setText(rightMenu);
            } else {
                textRight.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(leftMenu) && !TextUtils.isEmpty(centerMenu) && !TextUtils.isEmpty(rightMenu)) {// 全部都有
                viewOne.setVisibility(View.VISIBLE);
                viewTwo.setVisibility(View.VISIBLE);
            } else if (!TextUtils.isEmpty(leftMenu) && TextUtils.isEmpty(centerMenu) && !TextUtils.isEmpty(rightMenu)) {// 中间为空
                viewOne.setVisibility(View.VISIBLE);
                viewTwo.setVisibility(View.GONE);
            } else if (TextUtils.isEmpty(leftMenu) && !TextUtils.isEmpty(centerMenu) && !TextUtils.isEmpty(rightMenu)) {// 左边为空
                viewOne.setVisibility(View.GONE);
                viewTwo.setVisibility(View.VISIBLE);
            } else if (TextUtils.isEmpty(leftMenu) && !TextUtils.isEmpty(centerMenu) && !TextUtils.isEmpty(rightMenu)) {// 右边为空
                viewOne.setVisibility(View.VISIBLE);
                viewTwo.setVisibility(View.GONE);
            } else {// 只有一个
                viewOne.setVisibility(View.GONE);
                viewTwo.setVisibility(View.GONE);
            }

            textLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onMenuClick != null)
                        onMenuClick.onLeftMenuClick();
                    dialog.dismiss();
                }
            });
            textCenter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onMenuClick != null)
                        onMenuClick.onCenterMenuClick();
                    dialog.dismiss();
                }
            });
            textRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onMenuClick != null)
                        onMenuClick.onRightMenuClick();
                    dialog.dismiss();
                }
            });
            dialog.setCanceledOnTouchOutside(false);
            dialog.setContentView(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Dialog showPromptDialogIcon(Activity activity, String title, String content, String leftMenu,
                                              String centerMenu, String rightMenu, final OnMenuClick onMenuClick, String content2, int resid) {
        return showPromptDialogIcon(null, activity, title, content, leftMenu, centerMenu, rightMenu, onMenuClick,
                content2, resid);
    }

    public static Dialog showPromptDialogIcon(Dialog bdialog, Activity activity, String title, String content,
                                              String leftMenu, String centerMenu, String rightMenu, final OnMenuClick onMenuClick, String content2,
                                              int resid) {
        try {
            content = TextUtils.isEmpty(content) ? "系统提示" : content;
            final Dialog dialog;
            if (bdialog == null) {
                dialog = new Dialog(activity, R.style.dialog);
            } else {
                dialog = bdialog;
            }
            dialog.show();
            View view = LayoutInflater.from(ZnsbApp.mContext).inflate(R.layout.common_prompt_dialog, null);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
            TextView textTitle = (TextView) view.findViewById(R.id.tv_common_prompt_title);
            TextView textContent = (TextView) view.findViewById(R.id.tv_common_prompt_content);

            ImageView imgIcon = (ImageView) view.findViewById(R.id.tv_common_prompt_icon);
            TextView textContent2 = (TextView) view.findViewById(R.id.tv_common_prompt_content2);

            if (TextUtils.isEmpty(title)) {
                textTitle.setVisibility(View.GONE);
            } else {
                textTitle.setText(title);
                textTitle.setVisibility(View.VISIBLE);
            }
            if (TextUtils.isEmpty(content)) {
                textContent.setVisibility(View.GONE);
            } else {
                textContent.setText(content);
                textContent.setVisibility(View.VISIBLE);
            }

            if (TextUtils.isEmpty(content2)) {
                textContent2.setVisibility(View.GONE);
            } else {
                textContent2.setVisibility(View.VISIBLE);
                textContent2.setText(content2);
            }
            TextView textLeft = (TextView) view.findViewById(R.id.tv_common_prompt_left);
            TextView textCenter = (TextView) view.findViewById(R.id.tv_common_prompt_center);
            TextView textRight = (TextView) view.findViewById(R.id.tv_common_prompt_right);

            View viewOne = view.findViewById(R.id.view_one);
            View viewTwo = view.findViewById(R.id.view_two);

            if (!TextUtils.isEmpty(leftMenu)) {// 左菜单不为空
                textLeft.setVisibility(View.VISIBLE);
                textLeft.setText(leftMenu);
            } else {
                textLeft.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(centerMenu)) {
                textCenter.setVisibility(View.VISIBLE);
                textCenter.setText(centerMenu);
            } else {
                textCenter.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(rightMenu)) {
                textRight.setVisibility(View.VISIBLE);
                textRight.setText(rightMenu);
            } else {
                textRight.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(leftMenu) && !TextUtils.isEmpty(centerMenu) && !TextUtils.isEmpty(rightMenu)) {// 全部都有
                viewOne.setVisibility(View.VISIBLE);
                viewTwo.setVisibility(View.VISIBLE);
            } else if (!TextUtils.isEmpty(leftMenu) && TextUtils.isEmpty(centerMenu) && !TextUtils.isEmpty(rightMenu)) {// 中间为空
                viewOne.setVisibility(View.VISIBLE);
                viewTwo.setVisibility(View.GONE);
            } else if (TextUtils.isEmpty(leftMenu) && !TextUtils.isEmpty(centerMenu) && !TextUtils.isEmpty(rightMenu)) {// 左边为空
                viewOne.setVisibility(View.GONE);
                viewTwo.setVisibility(View.VISIBLE);
            } else if (TextUtils.isEmpty(leftMenu) && !TextUtils.isEmpty(centerMenu) && !TextUtils.isEmpty(rightMenu)) {// 右边为空
                viewOne.setVisibility(View.VISIBLE);
                viewTwo.setVisibility(View.GONE);
            } else {// 只有一个
                viewOne.setVisibility(View.GONE);
                viewTwo.setVisibility(View.GONE);
            }

            if (resid != -1) {
                imgIcon.setVisibility(View.VISIBLE);
                RotateAnimation rotateAnimation = new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF, .5f,
                        Animation.RELATIVE_TO_SELF, .5f);
                rotateAnimation.setDuration(1000);
                rotateAnimation.setInterpolator(new LinearInterpolator());
                rotateAnimation.setRepeatCount(Animation.INFINITE);
                imgIcon.startAnimation(rotateAnimation);
            } else {
                imgIcon.setVisibility(View.GONE);
            }

            textLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    if (onMenuClick != null)
                        onMenuClick.onLeftMenuClick();
                }
            });
            textCenter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    if (onMenuClick != null)
                        onMenuClick.onCenterMenuClick();
                }
            });
            textRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    if (onMenuClick != null)
                        onMenuClick.onRightMenuClick();
                }
            });
            dialog.setCanceledOnTouchOutside(false);
            dialog.setContentView(view);
            return dialog;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public interface OnMenuClick {
        public void onLeftMenuClick();

        public void onCenterMenuClick();

        public void onRightMenuClick();
    }
}
